package thread;

public class Bank {
    private int account = 20000;
    public synchronized boolean getMoney(int money){
        int sum = getAccount();
        if (sum>=money){
            sum-=money;
            //在这里线程的时间片用完,发生线程切换会出现并发安全问题
            Thread.yield();//一个线程调用该方法后就会立即释放本次剩余时间片
            saveMoney(sum);
            return true;//允许出钞
        }
        return false;//拒绝出钞
    }

    public void saveMoney(int account){
        this.account =  account;
    }
    public int getAccount(){
        return account;
    }
}
